package ru.fadeev.crammer.ui.window;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingConstants;


/**
 * TranscriptionPromptWindow class provides GUI for showing map between
 * transcription symbols and keyboard keys
 * @author Konstantin Fadeyev
 *
 */
public class TranscriptionPromptWindow extends JWindow {

	private Point mouseLocation;
    private String transPrompt = 
        "<html>" +
        	"<head>" +
        		"<style type='text/css'>" +
        		"<!--" +
        		".trans {font-family: LucidaSans}" +
        		".hyphen {margin-left: 5px; margin-right: 5px}" +
        		".colspace {margin-left: 8px; margin-right: 8px}" +   		
        		"-->" +
        		"</style>" +
        	"</head>" +
        	"<body>" +
            	"<table cellspacing=0 cellpadding=1 border=0 valign='top'>" +
            		"<tr>" +
            			"<td>" +
    					    "<table cellspacing=0 cellpadding=1>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x028c;</td>" +
    					    "<td class='hyphen'> - </td>" +
    					    "<td>a</td>" +
    					    "</tr>" +					    
    					    "<tr>" +
    					    "<td class='trans'>&#x0259;</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>q</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x00e6;</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>x</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x026a;</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>i</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>e</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>e</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x0252;</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>o</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x028a;</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>u</td>" +
    					    "</tr>" +
    					    "</table>" +    
    					"</td>" +
    					"<td class='colspace'>" +
    					"</td>" +
    					"<td>" +
    						"<table cellspacing=0 cellpadding=1>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x0251;&#x02d0;</td>" +
    					    "<td class='hyphen'> - </td>" +
    					    "<td>R</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x025c;&#x02d0;</td>" +
    					    "<td class='hyphen'> - </td>" +
    					    "<td>Q</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>i&#x02d0;</td>" +
    					    "<td class='hyphen'> - </td>" +
    					    "<td>I</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x0254;&#x02d0;</td>" +
    					    "<td class='hyphen'> - </td>" +
    					    "<td>O</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>u&#x02d0;</td>" +
    					    "<td class='hyphen'> - </td>" +
    					    "<td>U</td>" +
    					    "</tr>" +
    					    "</table>" +    
    					"</td>" +
    					"<td class='colspace'>" +
    					"</td>" +
    					"<td>" +
    						"<table cellspacing=0 cellpadding=1>" +
    					    "<tr>" +
    					    "<td class='trans'>a&#x028a;</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>H</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>a&#x026a;</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>y</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x0254;&#x026a;</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>Y</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>e&#x026a;</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>E</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x0259;&#x028a;</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>W</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>e&#x0259;</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>X</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x026a;&#x0259;</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>F</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x028a;&#x0259;</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>A</td>" +
    					    "</tr>" +
    					    "</table>" +    
    					"</td>" +
    					"<td class='colspace'>" +
    					"</td>" +
    					"<td>" +
    						"<table cellspacing=0 cellpadding=1>" +
    					    "<tr>" +
    					    "<td class='trans'>b</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>b</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>d</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>d</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>f</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>f</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>g</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>g</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>h</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>h</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>j</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>j</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>k</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>k</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>l</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>l</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>m</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>m</td>" +
    					    "</tr>" +
    					    "</table>" +    
    					"</td>" +
    					"<td class='colspace'>" +
    					"</td>" +
    					"<td>" +
    						"<table cellspacing=0 cellpadding=1>" +
    					    "<tr>" +
    					    "<td class='trans'>n</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>n</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>p</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>p</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>r</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>r</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>s</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>s</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>t</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>t</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>v</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>v</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>w</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>w</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>z</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>z</td>" +
    					    "</tr>" +
    					    "</table>" +    
    					"</td>" +
    					"<td class='colspace'>" +
    					"</td>" +
    					"<td>" +
    						"<table cellspacing=0 cellpadding=1>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x0292;</td>" +
    					    "<td class='hyphen'> - </td>" +
    					    "<td>G</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x02a4;</td>" +
    					    "<td class='hyphen'> - </td>" +
    					    "<td>J</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x0283;</td>" +
    					    "<td class='hyphen'> - </td>" +
    					    "<td>S</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x02a7;</td>" +
    					    "<td class='hyphen'> - </td>" +
    					    "<td>C</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x03b8;</td>" +
    					    "<td class='hyphen'> - </td>" +
    					    "<td>T</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x00f0;</td>" +
    					    "<td class='hyphen'> - </td>" +
    					    "<td>D</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x014b;</td>" +
    					    "<td class='hyphen'> - </td>" +
    					    "<td>N</td>" +
    					    "</tr>" +						
    					    "<tr>" +
    					    "<td class='trans'>&#x02c8;</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>'</td>" +
    					    "</tr>" +
    					    "<tr>" +
    					    "<td class='trans'>&#x02cc;</td>" +
    					    "<td class='hyphen'>-</td>" +
    					    "<td>\"</td>" +
    					    "</tr>" +
    					    "</table>" +
    					"</td>" +
    				"</tr>" +  			
    			"</table>" +
    		"</body>" +
        "</html>";
    

    /**
     * Constructs TranscriptionPromptWindow
     * @param owner parent window
     */
	public TranscriptionPromptWindow(Window owner) {
		super(owner);
    	setSize(400, 190);

     	JPanel mainPanel = new JPanel();
     	mainPanel.setBorder(BorderFactory.createLineBorder(Color.black));
     	Color bgColor = new Color(255, 255, 221); //light yellow
     	mainPanel.setBackground(bgColor); 
     	JLabel mainLabel = new JLabel();
     	mainLabel.setHorizontalAlignment(SwingConstants.CENTER);
     	mainLabel.setVerticalAlignment(SwingConstants.TOP);
     	mainLabel.setText(transPrompt);
     	
     	ImageIcon closeIcon = new ImageIcon("icons/8/closePrompt.png");
		JButton closeButton = new JButton(closeIcon); 
		closeButton.setToolTipText("Close");
		closeButton.setBorder(null);
		closeButton.setBackground(bgColor);
		closeButton.setFocusable(false);
		closeButton.addActionListener(
			    new AbstractAction() {
			        public void actionPerformed(ActionEvent ev) {
			        	setVisible(false);
			        }
			    });
		JPanel titlePanel = new JPanel();
		titlePanel.setBackground(bgColor);
				
		//Placing components
		mainPanel.setLayout(new BorderLayout());
		mainPanel.add(titlePanel, BorderLayout.NORTH);
     	mainPanel.add(mainLabel);  

		titlePanel.setPreferredSize(new Dimension(12, 12)); 
		titlePanel.setLayout(new BorderLayout());
		titlePanel.add(closeButton, BorderLayout.EAST);	

		closeButton.setPreferredSize(new Dimension(12, 12));

     	setContentPane(mainPanel);
     	
    	addMouseListener(
    			new MouseAdapter() {
    				public void mousePressed(MouseEvent e) {
    					mouseLocation = e.getComponent().getLocationOnScreen();
    					mouseLocation.translate(e.getPoint().x, e.getPoint().y);
    				}
    				
    				public void mouseClicked(MouseEvent e) {
    					if(e.getButton() == MouseEvent.BUTTON3) {
    						setVisible(false);
    					}
    				}
    			});
    	
    	addMouseMotionListener(
    			new MouseMotionAdapter() {
    				 public void mouseDragged(MouseEvent e) {
    					 int x = getX();
    					 int y = getY();
    					 Point compLoc = e.getComponent().getLocationOnScreen();
    					 setLocation(x + e.getX() + compLoc.x - mouseLocation.x, 
    							 y + e.getY() + compLoc.y - mouseLocation.y);
    					 mouseLocation = compLoc;
     					 mouseLocation.translate(e.getPoint().x, e.getPoint().y);
    				 }
    			});
	}
}
